//路由文件
var express=require('express')
var router=express.Router();
var data = require("./sqlcon.js");
var config=require('./config.js');
//首页数据
var news=require('./data/news.js')
var List=require('./data/List.js')
var List2=require('./data/List2.js')
var JiluList=require('./data/JiluList.js')
var xiangqing0=require('./data/xiangqing0.js')
var xiangqing1=require('./data/xiangqing1.js')
var url = require("url");//路径上携带参数

//健身数据
var trainer = require("./data2/trainer.js");
var course = require("./data2/course.js");
var fodder = require("./data2/fodder.js");
var part = require("./data2/part.js");
var apparatus = require("./data2/apparatus.js");
var difficulty = require("./data2/difficulty.js");

//首页数据请求
router.get('/news',function(req,res){
	res.send(news);
})
//get请求
router.get('/list',function(req,res){
	res.send(List);
})
//get请求
router.get('/list2',function(req,res){
	res.send(List2);
})
//get请求
router.get('/jiluList',function(req,res){
	res.send(JiluList);
})
//get请求
router.get('/xiangqing0',function(req,res){
	var query = url.parse(req.url,true).query;
	var nowId=query.id;
	if(nowId == 0){
		res.send(xiangqing0);
	}else if(nowId == 1){
		res.send(xiangqing1);
	}
})
//登录
router.post(config.interfaceUrl.denglu,function(req,res){
	var username = req.body.username;
  	var password = req.body.password;
	var sql="SELECT * FROM users WHERE `username`=? AND `password`=?";
	var params = [username,password]
	data(sql,params,function(result){
		if(result.length > 0){
			res.send({
				result:result,
				msg:"登录成功"
			});
		}else{
			res.send({
		        msg:"用户名密码不存在",
		        code:"4004"
		    })
		}
	})
})
//注册
router.post(config.interfaceUrl.zhuce,function(req,res){
	var username = req.body.username;
  	var password = req.body.password;
  	var sql = "INSERT INTO users values(?,?,null)";
	var params = [username,password]
	data(sql,params,function(result){
		if(result.affectedRows){
			res.send({
		       msg:"注册成功",
		       code:"200"
		    })
		}else{
			res.send({
		       msg:"注册失败",
		       code:"404"
		    })
		}
	})
})
//健身数据请求
router.get("/trainer",function(req,res){
	res.send(trainer)
  })
  
  router.get("/course",function(req,res){
	res.send(course)
  })
  
  router.get("/fodder",function(req,res){
	res.send(fodder)
  })
  
  router.get("/part",function(req,res){
	res.send(part)
  })
  
  router.get("/apparatus",function(req,res){
	res.send(apparatus)
  })
  
  router.get("/difficulty",function(req,res){
	res.send(difficulty)
  })

module.exports=router 